import { createRouter, createWebHashHistory } from 'vue-router'
import { NProgress } from './ui'
import routes from '@/routes'

// 载入路由
const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach(async (to, from, next) => {
  NProgress.start()

  // 检测使用看守器
  if (to.meta.guard) {
    try{
      if(!await to.meta.guard(to.meta.permission)) {
        return next({ name: '403' })
      }
    } catch (error) {
      console.log(error)
      return next(false)
    }
  }

  next()
})
  
router.afterEach(() => {
  NProgress.done()
})

export default router